<?php

/*鉴黄管理*/
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class ReflectController extends AdminbaseController {
    protected function getStatus($k=''){
        $status=[
            '0'=>'待处理',
            '1'=>'已处理',
        ];
        
        if($k==''){
            return $status;
        }
        return $status[$k];
    }
	
	
	
	//直播截图
	public function screenshot(){
		$data = $this->request->param();
        $map=[];
		
		
		$stream_id=isset($data['stream_id']) ? $data['stream_id']: '';
        if($stream_id!=''){
            $map[]=['stream_id','=',$stream_id];
        }	
		
    	$lists = Db::name("user_screenshot")
                ->where($map)
                ->order("id DESC")
                ->paginate(20);
        
		$lists->appends($data);
        $page = $lists->render();
    	$this->assign('lists', $lists);
    	$this->assign("page", $page);

		return $this->fetch();
	}
	
	// 直播截图批量删除
	public function screenshot_deletes(){
		$data = input();
		foreach ($data['ids'] as $k => $r) {
            Db::name('user_screenshot')->where(['id'=>$r])->delete();
        }
		$status = true;
		if ($status) {
			$this->success("操作成功！");
		} else {
			$this->error("操作失败！");
		}

	}
	
	
	
	//鉴黄记录
	public function yellow(){
		$data = $this->request->param();
        $map=[];

		$start_time=isset($data['start_time']) ? $data['start_time']: '';
        $end_time=isset($data['end_time']) ? $data['end_time']: '';
        
        if($start_time!=""){
           $map[]=['screenshottime','>=',strtotime($start_time)];
        }

        if($end_time!=""){
           $map[]=['screenshottime','<=',strtotime($end_time) + 60*60*24];
        }
        
        $status=isset($data['status']) ? $data['status']: '';
        if($status!=''){
            $map[]=['status','=',$status];
        }
        
        $keyword=isset($data['keyword']) ? $data['keyword']: '';
        if($keyword!=''){
            $map[]=['userid|touserid|roomid','like',"%".$keyword."%"];
        }
		
    	$lists = Db::name("user_jianhuang")
                ->where($map)
                ->order("id DESC")
                ->paginate(20);
		$lists->each(function($v,$k){
            $userinfo=getUserInfo($v['userid']);
			$user_status=Db::name("user")->where("id={$v['userid']}")->value('user_status');  
            $userinfo['user_status']=$user_status;
			$v['userinfo']=$userinfo;
			
			
			$touserinfo=getUserInfo($v['touserid']);
			$touser_status=Db::name("user")->where("id={$v['touserid']}")->value('user_status');  
            $touserinfo['user_status']=$touser_status;
			$v['touserinfo']=$touserinfo;
            return $v;           
        });
        
		$lists->appends($data);
        $page = $lists->render();
    	$this->assign('lists', $lists);
    	$this->assign("page", $page);
    	$this->assign("status", $this->getStatus());

		return $this->fetch();
	}
	
	
    function setstatus(){
        $id = $this->request->param('id', 0, 'intval');

        $data=[
            'status'=>1,
            'uptime'=>time(),
        ];
        $rs = Db::name('user_jianhuang')->where("id={$id}")->update($data);
        if($rs===false){
            $this->error("标记失败！");
        }

        $this->success("标记成功！");
        							  		
    }
	
	
	
	public function ban(){
		$data = $this->request->param();
        if ($data) {
			$id=$data['id'];
			
			$info=Db::name('user_jianhuang')->where("id={$id}")->find();
			if($info){
				$uid=$info['userid'];
				
			
				$result = Db::name("user")->where(["id" => $uid, "user_type" => 2])->update(['user_status'=>0]);
				if ($result) {
					$key2="token_".$uid;
					delcache($key2);
					Db::name("user_token")->where(["user_id" => $uid])->delete();
					$data=array(
						"uid"=>$uid, 
						"addtime"=>time(), 
						"expiretime"=>'0', 
						"count"=>1, 
					);
					
					//查看是否有拉黑记录
					$reflect=Db::name('user_reflect_block')
						->where("uid={$uid}")
						->find();
					if($reflect){
						Db::name("user_reflect_block")
							->where("uid={$uid}")
							->update($data);
					}else{
						Db::name('user_reflect_block')->insert($data);
					}
				}
				
				
				
				$touid=$info['touserid'];
				$result = Db::name("user")->where(["id" => $touid, "user_type" => 2])->update(['user_status'=>0]);
				if ($result) {
					$key2="token_".$touid;
					delcache($key2);
					Db::name("user_token")->where(["user_id" => $touid])->delete();
					$data=array(
						"uid"=>$touid, 
						"addtime"=>time(), 
						"expiretime"=>'0', 
						"count"=>1, 
					);
					
					//查看是否有拉黑记录
					$reflect=Db::name('user_reflect_block')
						->where("uid={$touid}")
						->find();
					if($reflect){
						Db::name("user_reflect_block")
							->where("uid={$touid}")
							->update($data);
					}else{
						Db::name('user_reflect_block')->insert($data);
					}
				}
				
				
				
				$data=[
					'status'=>1,
					'uptime'=>time(),
				];
				Db::name('user_jianhuang')->where("id={$id}")->update($data);
				$this->success("操作成功！");
			}else{
				$this->error('数据传入失败！');
			}

        }else{
            $this->error('数据传入失败！');
        }
    }
	
	
	
	//封禁记录
	public function ureflect_block(){
		$data = $this->request->param();
        $map=[];


        
        $keyword=isset($data['keyword']) ? $data['keyword']: '';
        if($keyword!=''){
            // $map[]=['uid','like',"%".$data['keyword']."%"];
			 $map[]=['uid','=',$keyword];
        }
		
    	$lists = Db::name("user_reflect_block")
                ->where($map)
                ->order("addtime DESC")
                ->paginate(20);
		$lists->each(function($v,$k){
            $userinfo=getUserInfo($v['uid']);

			$v['userinfo']=$userinfo;
			

            return $v;           
        });
        
		$lists->appends($data);
        $page = $lists->render();
    	$this->assign('lists', $lists);
    	$this->assign("page", $page);
    	$this->assign("status", $this->getStatus());

		return $this->fetch();
	}
	
    
}
